<template>
  <page-form-row>
    <!-- 是否租车押金抵扣 -->
    <!-- 租车押金应收已收，且剩余金额不为0 -->
    <!-- 
      NOT_RECEIVED(10, "应收未收"),
      RECEIVED(30, "应收已收"),
      WANT_DRAWBACK(50, "应退未退"),
      FINISH_DRAWBACK(70, "应退已退"), 
      // 租车押金状态 carDepositStatus
      // 违章押金状态 illegalDepositStatus
    -->
    <page-form-item size="1/2" label="是否租车押金抵扣" v-if="[30, 50].includes(depositConfig.carDepositStatus)">
      <a-radio-group v-model="form.depositDeductFlag">
        <a-radio :value="1">是</a-radio>
        <a-radio :value="0">否</a-radio>
      </a-radio-group>
    </page-form-item>
    <!-- 租车押金是否转违章押金 -->
    <!-- 租车押金应收已收，且剩余金额不为0，且违章押金应收未收 -->
    <page-form-item
      size="1/2"
      label="租车押金是否转违章押金"
      v-if="[30].includes(depositConfig.carDepositStatus) && [10].includes(depositConfig.illegalDepositStatus) && leftCarRentAmount > 0"
    >
      <a-radio-group v-model="form.changeDepositFlag">
        <a-radio :value="1">是</a-radio>
        <a-radio :value="0">否</a-radio>
      </a-radio-group>
    </page-form-item>
  </page-form-row>
  <page-form-row>
    <!-- 详细费用 -->
    <!-- 退租车押金 -->
    <page-form-item label="退租车押金" size="1/2">
      <i-input-number disabled :model-value="depositConfig.carDeposit || 0" />
    </page-form-item>
    <page-form-item label="退违章押金" size="1/2">
      <i-input-number disabled :model-value="depositConfig.illegalDeposit || 0" />
    </page-form-item>
    <!-- 其他押金无交互，仅作展示 -->
    <template v-if="depositConfig.depositList && depositConfig.depositList.length > 0">
      <page-form-item :label="item.depositTypeName" size="1/2" v-for="(item, index) in depositConfig.depositList" :key="index">
        <!-- 当前可用押金 -->
        <i-input-number disabled v-model="item.currentlyAvailableDepositAmount" />
      </page-form-item>
    </template>
    <page-form-item class="mb-0" label="退押金备注" size="1">
      <i-input v-model="form.backDepositRemark" />
    </page-form-item>
  </page-form-row>
</template>
<script setup>
  import { reactive, inject, computed, watch } from 'vue'
  import { queryActiveAmount } from '@/api/order/czsOrder'
  import { decimalAddition, decimalSubtract } from '@/utils'
  import { buildTreeData, processTreeData } from '../../components/order-fee/fee-statistic.js'
  const form = inject('form')
  const props = defineProps({
    orderId: [String, Number, null],
  })

  // 押金
  const depositConfig = reactive({
    carDeposit: null,
    illegalDeposit: null,
    depositList: [],
  })

  watch(
    () => props.orderId,
    (orderId) => {
      if (orderId) {
        // 获取当前可用押金
        queryActiveAmount({ orderId }).then((res) => {
          const result = res.result
          // 租车押金
          depositConfig.carDeposit = result.carDepositAmount
          // 自定义租车押金
          depositConfig.customDepositAmount = result.customDepositAmount
          // 违章押金
          depositConfig.illegalDeposit = result.illegalDepositAmount
          // 其他押金
          depositConfig.depositList = result.depositList

          // 租车押金状态
          depositConfig.carDepositStatus = result.carDepositStatus
          // 违章押金状态
          depositConfig.illegalDepositStatus = result.illegalDepositStatus
        })
      }
    },
    { immediate: true }
  )

  const feeForm = inject('feeForm')

  // 当前录入的费用合计（为正或者为负）
  const sumFee = computed(() => {
    let orderFee = feeForm.value.orderFee
    let sum = 0
    if (orderFee) {
      let data = buildTreeData(orderFee)
      let feeTreeStData = processTreeData(data)
      if (feeTreeStData.length > 0) {
        feeTreeStData.forEach((item) => {
          sum = decimalAddition(sum, item.value)
        })
      }
    }
    return sum
  })
  // 押金抵扣金额
  const depositDeductAmount = computed(() => {
    // 租车押金金额
    let carDeposit = depositConfig.carDeposit
    // 是否押金抵扣
    if (form.value.depositDeductFlag && sumFee.value > 0) {
      if (carDeposit > sumFee.value) {
        return sumFee.value
      } else {
        return carDeposit
      }
    }
    return 0
  })
  watch(
    () => depositDeductAmount.value,
    (newVal) => {
      form.value.depositDeductAmount = newVal
    },
    { immediate: true }
  )
  // 剩余的租车押金
  const leftCarRentAmount = computed(() => {
    // 当前押金
    let carDeposit = depositConfig.carDeposit || 0
    // 自定义押金
    let customDepositAmount = depositConfig.customDepositAmount || 0
    // 押金合计
    let sumDepositAmount = decimalAddition(carDeposit, customDepositAmount)
    return decimalSubtract(sumDepositAmount, depositDeductAmount.value)
  })
  // 转违章金额
  const changeDepositAmount = computed(() => {
    // 转违章押金开关开启
    if (form.value.changeDepositFlag) {
      return leftCarRentAmount.value
    } else {
      return 0
    }
  })
  watch(
    () => leftCarRentAmount.value,
    (newVal) => {
      if (newVal <= 0 && form.value.changeDepositFlag) {
        form.value.changeDepositFlag = 0
      }
    }
  )
  watch(
    () => changeDepositAmount.value,
    (newVal) => {
      form.value.changeDepositAmount = newVal
    },
    { immediate: true }
  )
</script>
<style lang="less" scoped></style>
